(function (vc) {
    vc.extends({
        data: {
            printSmallOrderInfo: {
                shopName: '',
                amount: 0.00,
                orderCart: [],
                createTime: '',
                orderId: '',
                cartNumCount: 0,
                totalPayPrice: 0.0,
                totalPrice: 0.0,
                orderPayPrice: 0.0,
                content: '',
                qrImg: '',
                autoPrint: 'N',
                discountPayPrice: 0.0
            },
            printFlag: '0'
        },
        _initMethod: function () {
            $that.printSmallOrderInfo.orderId = vc.getParam('orderId');
            $that.printSmallOrderInfo.autoPrint = vc.getParam('autoPrint');
            $that.printSmallOrderInfo.shopName = vc.getCurrentCommunity().shopName;
            $that._loadReceipt();
            $that._loadPrintSpec();
        },
        _initEvent: function () {
            vc.on('printSmallOrder', 'print', function () {
                setTimeout('$that._printPurchaseApplyDiv()', 1000);
            })
        },
        methods: {
            _loadReceipt: function () {
                var param = {
                    params: {
                        page: 1,
                        row: 100,
                        orderId: $that.printSmallOrderInfo.orderId,
                        shopId: vc.getCurrentCommunity().shopId
                    }
                };
                //发送get请求
                vc.http.apiGet('/storeOrder.queryStoreOrderCart',
                    param,
                    function (json, res) {
                        var _orderCartObj = JSON.parse(json);
                        let _orderCart = _orderCartObj.data;
                        $that.printSmallOrderInfo.orderCart = _orderCart;
                        let _cartNumCount = 0;
                        let _totalPayPrice = 0.0;
                        _orderCart.forEach(item => {
                            _cartNumCount += parseFloat(item.cartNum);
                            //_totalPayPrice += parseFloat(item.payPrice);
                        });
                        $that.printSmallOrderInfo.cartNumCount = _cartNumCount;
                        $that.printSmallOrderInfo.discountPayPrice = (parseFloat(_orderCart[0].totalPrice) - parseFloat(_orderCart[0].orderPayPrice)).toFixed(2);
                        $that.printSmallOrderInfo.orderPayPrice = _orderCart[0].orderPayPrice;
                        $that.printSmallOrderInfo.createTime = _orderCart[0].createTime;
                        if ($that.printSmallOrderInfo.autoPrint == 'Y') {
                            vc.emit('printSmallOrder', 'print', {});
                        }
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _printPurchaseApplyDiv: function () {
                let bdhtml = window.document.body.innerHTML;
                let sprnstr = "<startprint></startprint>";
                let eprnstr = "<endprint></endprint>";
                let prnhtml = bdhtml.substr(bdhtml.indexOf(sprnstr) + sprnstr.length);
                prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr));
                window.document.body.innerHTML = prnhtml;
                console.log(window.document.body.innerHTML)
                window.print();
                // window.print();
                // //$that.printFlag = false;
                window.opener = null;
                window.close();
            },
            _printCloudPurchaseApplyDiv: function () {
                let _paramIn = {
                    shopId: vc.getCurrentCommunity().shopId,
                    orderId: $that.printSmallOrderInfo.orderId
                }
                vc.http.apiPost(
                    '/printer.orderPrinter',
                    JSON.stringify(_paramIn),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            vc.toast(_json.msg);
                            vc.goBack();
                            return;
                        }
                        vc.toast(_json.msg);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            _loadPrintSpec: function () {
                var param = {
                    params: {
                        page: 1,
                        row: 1,
                        specCd: 2020,
                        communityId: vc.getCurrentCommunity().shopId
                    }
                };
                //发送get请求
                vc.http.apiGet('/feePrintSpec/queryFeePrintSpec',
                    param,
                    function (json, res) {
                        var _json = JSON.parse(json);
                        var _data = _json.data;
                        if (_data.length > 0) {
                            $that.printSmallOrderInfo.content = _data[0].content;
                            $that.printSmallOrderInfo.qrImg = _data[0].qrImg;
                            $that.printSmallOrderInfo.shopName = _data[0].printName;
                        }
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _closePage: function () {
                window.opener = null;
                window.close();
            }
        }
    });
})(window.vc);
